<?php


namespace app\model\partner;


use app\model\cart\VerifyCartShapeModel;
use app\model\config\ConfigOrderSourceModel;
use app\model\config\RegionModel;
use app\model\school\SchoolBaseModel;
use app\model\school\SchoolModel;
use app\model\user\UserModel;
use think\Model;
use think\model\concern\SoftDelete;

class EscortMenuReserveOrderModel extends Model
{
    protected $name = 'escort_menu_reserve_order';
    protected $autoWriteTimestamp = true;

    use SoftDelete;
    protected $defaultSoftDelete = 0;


    const WAIT_ASSIGN_ORDER = 1;// 待派单
//    const WAIT_CONFIRM = 2;// 待确认
    const WAIT_SERVICE = 3;// 待服务
//    const WAIT_EVAL = 4;// 待评价
    const COMPLETE = 5;// 已完成
    const CANCEL = 6;// 已取消
    const WAIT_CHECK_COUPON = 7;// 待验券
    const WAIT_UPLOAD = 8;// 待上传券码

    protected $orderStatusArr = [
        1 => '待派单',
//        2 => '待确认',
        3 => '待服务',
//        4 => '待评价',
        5 => '已完成',
        6 => '已取消',
        7 => '待验券',
        8 => '待上传券码'
    ];

    protected $conditionArr = [
        0 => '',
        1 => '新车',
        2 => '二手车'
    ];

    protected $carTypeArr = [
        0 => '',
        1 => '油车',
        2 => '电车',
        3 => '混动',
    ];

    protected $append = ['order_status_txt', 'reserve_time_txt', 'condition_txt', 'car_type_txt'];

    // 订单状态数组
    public function getStatusArr()
    {
        return $this->orderStatusArr;
    }

    public function getReserveTimeTxtAttr($value, $data)
    {
        if (!isset($data['reserve_time'])) {
            return '';
        }
        return $data['reserve_time'] == 0 ? '' : date('Y-m-d H:i:s', $data['reserve_time']);
    }

    // 订单状态文本
    public function getOrderStatusTxtAttr($value, $data)
    {
        if (!isset($data['status'])) {
            return '';
        }
        return $this->orderStatusArr[$data['status']];
    }

    public function getConditionTxtAttr($value, $data)
    {
        if (!isset($data['condition'])) {
            return '';
        }
        return $this->conditionArr[$data['condition']];
    }

    public function getCarTypeTxtAttr($value, $data)
    {
        if (!isset($data['cart_type'])) {
            return '';
        }
        return $this->carTypeArr[$data['cart_type']];
    }

    // 用户
    public function user()
    {
        return $this->hasOne(UserModel::class, 'id', 'user_id')->bind(['user_mobile' => 'mobile']);
    }

    // 教练
    public function partner()
    {
        return $this->hasOne(UserPartnerModel::class, 'id', 'coach_user_id')->bind([
            'partner_mobile' => 'mobile',
            'partner_name' => 'username',
            'partner_trial_class_amount' => 'trial_class_amount',
            'partner_class_amount' => 'class_amount',
            'service_num'
        ]);
    }

    // 城市
    public function city()
    {
        return $this->hasOne(RegionModel::class, 'id', 'city_id')->bind(['city_name' => 'name']);
    }

    public function img()
    {
        return $this->hasOne(EscortMenuOrderModel::class, 'id', 'order_id')->bind(['menu_img']);
    }

    public function menu()
    {
        return $this->hasOne(EscortMenuModel::class, 'id', 'menu_id');
    }

    public function menuOrder()
    {
        return $this->hasOne(EscortMenuOrderModel::class, 'id', 'order_id')
            ->bind([
                'menu_total' => 'total',
                'used_duration',
                'menu_name',
                'menu_price',
                'pay_amount',
                'coach_amount',
                'amount',
                'menu_image',
            ]);
    }

    public function source()
    {
        return $this->hasOne(ConfigOrderSourceModel::class, 'id', 'source_id')->bind(['source_title' => 'title']);
    }
}